//
//  LocalNotification.m
//  ChatKit-OC
//
//  Created by th on 2017/4/6.
//  Copyright © 2017年 ElonChan. All rights reserved.
//

#import "LocalNotification.h"

@implementation LocalNotification

+ (instancetype)shareInstance {
    static LocalNotification *noti = nil;
    
    static dispatch_once_t onceToken;
    
    dispatch_once(&onceToken, ^{
        noti = [[self alloc] init];
    });
    
    return noti;
}

+ (void)notifiWithContent:(NSString *)content {
    // 1.创建本地通知
    UILocalNotification *localNote = [[UILocalNotification alloc] init];
    
    // 2.设置本地通知的内容
    // 2.1.设置通知发出的时间
    localNote.fireDate = [NSDate dateWithTimeIntervalSinceNow:1.0];
    // 2.2.设置通知的内容
    localNote.alertBody = content;
    
    localNote.alertAction = content;
    
    // 2.6.设置alertTitle
    localNote.alertTitle = @"你有一条新消息";
    
    // 使用userInfo来配置自定义信息 (区分每个通知对象)
    localNote.userInfo = @{
                              @"key" : kmonitoringWithNotification,
                              };
    
    // 3.调用通知
    [[UIApplication sharedApplication] scheduleLocalNotification:localNote];
}

+ (void)removeLocalNotification {
    
    // 1. 找到通知调度池中所有的本地通知 (只能取到App自己的本地通知)
    NSArray *notificationArr = [[UIApplication sharedApplication] scheduledLocalNotifications];
    
    // 2. 遍历, 找到要删除的通知
    for (UILocalNotification *notification in notificationArr) {
        [notification.userInfo enumerateKeysAndObjectsUsingBlock:^(NSString *key, NSString * obj, BOOL * _Nonnull stop) {
            
            // 匹配key值
            if ([key isEqualToString:@"key"] && [obj isEqualToString:kmonitoringWithNotification]) {
                
                // notification就要删除的通知, 移除指定的通知
                [[UIApplication sharedApplication] cancelLocalNotification:notification];
                
                // 找到了需要的notifcation, 遍历停下来
                *stop = YES;
            }
        }];
    }
}

@end
